package com.fl.sliderview;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 滑块验证的view ui
 * 2023年06月03日14:57:31
 */
public class SliderVerificationView extends FrameLayout {

    // 滑块view
    private ImageAuthenticationView mAuthView;
    // 验证成功的view
    private LinearLayout mAuthSuccessContainer;
    // 验证失败的view
    private LinearLayout mAuthErrorContainer;
    // 滑块seekbar
    private TextSeekbar mSeekbar;

    private long _slideStartTime;// 滑动开始时间
    private long _slideEndTime;// 滑动结束时间

    public SliderVerificationView(@NonNull Context context) {
        super(context);
        init();
    }

    public SliderVerificationView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SliderVerificationView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.slider_verification_view, this, false);
        mAuthView = view.findViewById(R.id.mAuthView);
        mAuthSuccessContainer = view.findViewById(R.id.mAuthSuccessContainer);
        mAuthErrorContainer = view.findViewById(R.id.mAuthErrorContainer);
        mSeekbar = view.findViewById(R.id.mSeekbar);
        addView(view);

        initEvent();

    }


    private void initEvent() {
        mAuthView.setPuzzleListener(new ImageAuthenticationView.onPuzzleListener() {
            @Override
            public void onSuccess() {
                mAuthSuccessContainer.setVisibility(VISIBLE);
                if (mListener != null) {
                    mListener.onVerificationSuccess(_slideEndTime - _slideStartTime);
                }
            }

            @Override
            public void onFail() {
                mAuthErrorContainer.setVisibility(VISIBLE);
                mAuthErrorContainer.postDelayed(() -> mAuthErrorContainer.setVisibility(GONE), 1000);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    mSeekbar.setProgress(0, true);
                } else {
                    mSeekbar.setProgress(0);
                }
                mAuthView.reSet();
                if (mListener != null) {
                    mListener.onVerificationError(_slideEndTime - _slideStartTime);
                }
            }
        });

        mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                Log.d("fanlei","最大："+seekBar.getMax());
                Log.d("fanlei","比例："+i);
                Log.d("fanlei","距离："+mAuthView.getAverageDistance(seekBar.getMax())* i);
                mAuthView.setUnitMoveDistance(mAuthView.getAverageDistance(seekBar.getMax())* i);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                _slideStartTime = System.currentTimeMillis();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                _slideEndTime = System.currentTimeMillis();
                mAuthView.testPuzzle();
            }
        });
    }


    private OnVerificationResultListener mListener;

    public interface OnVerificationResultListener {
        void onVerificationSuccess(long SlideDuration);

        void onVerificationError(long SlideDuration);
    }

    public void setOnVerificationResultListener(OnVerificationResultListener listener) {
        this.mListener = listener;
    }

}
